<template>
  <div class="dashboard-container home">
    <div class="container">
      <h2 class="homeTitle">
        今日数据<i>{{ todayDate }}</i>
        <router-link to="/statistics"><span>详细数据</span></router-link>
      </h2>
      <div class="overviewBox">
        <ul>
          <li>
            <p class="tit">营业额</p>
            <p class="num">¥ {{ todayData.turnover }}</p>
          </li>
          <li>
            <p class="tit">有效订单</p>
            <p class="num">{{ todayData.validOrderCount }}</p>
          </li>
          <li>
            <p class="tit">订单完成率</p>
            <p class="num">
              {{
                todayData.orderCompletionRate !== undefined && todayData.orderCompletionRate !== null
                  ? (todayData.orderCompletionRate * 100).toFixed(2)
                  : 0
              }}%
            </p>
          </li>
          <li>
            <p class="tit">平均客单价</p>
            <p class="num">¥ {{ todayData.unitPrice }}</p>
          </li>

          <li>
            <p class="tit">新增用户</p>
            <p class="num">{{ todayData.newUsers }}</p>
          </li>
        </ul>
      </div>
    </div>


    <div class="container">
      <h2 class="homeTitle">
        订单管理<i>{{ todayDate }}</i><router-link to="/order"><span>订单明细</span></router-link>
      </h2>
      <div class="orderviewBox">
        <ul>
          <li>
            <span class="status"><i class="iconfont icon-complete"><el-icon>
                  <CircleCheckFilled />
                </el-icon></i>待完成</span>
            <span class="num">{{ ordersData.waitingOrders }}</span>
          </li>
          <li>
            <span class="status"><i class="iconfont icon-complete"><el-icon>
                  <Checked />
                </el-icon></i>已完成</span>
            <span class="num">{{ ordersData.completedOrders }}</span>
          </li>
          <li>
            <span class="status"><i class="iconfont icon-cancel"><el-icon>
                  <Failed />
                </el-icon></i>已取消</span>
            <span class="num">{{ ordersData.cancelledOrders }}</span>
          </li>
          <li>
            <span class="status">
              <i class="iconfont icon-all"><el-icon>
                  <List />
                </el-icon></i>
              全部订单</span>
            <router-link to="/order"><span class="num">{{ ordersData.allOrders }}</span></router-link>
          </li>
        </ul>
      </div>
    </div>

    <div class="homeMain">
      <div class="container">
        <h2 class="homeTitle">
          部门总览<router-link to="/department"><span>部门管理</span></router-link>
        </h2>
        <div class="orderviewBox">
          <ul>
            <li>
              <span class="status"><i class="iconfont icon-open"><el-icon><Select /></el-icon></i>已启用</span>
              <span class="num">{{ departmentData.sold }}</span>
            </li>
            <li>
              <span class="status"><i class="iconfont icon-stop"><el-icon>
                    <SemiSelect />
                  </el-icon></i>已停用</span>
              <span class="num">{{ departmentData.discontinued }}</span>
            </li>
            <li class="add">
              <router-link to="/department">
                <i></i>
                <p>新增部门</p>
              </router-link>
            </li>
          </ul>
        </div>
      </div>


      <div class="container">
        <h2 class="homeTitle">
          医护总览<router-link to="/staff"><span>医护管理</span></router-link>
        </h2>
        <div class="orderviewBox">
          <ul>
            <li>
              <span class="status"><i class="iconfont icon-open"><el-icon><Select /></el-icon></i>已启用</span>
              <span class="num">{{ staffData.sold }}</span>
            </li>
            <li>
              <span class="status"><i class="iconfont icon-stop"><el-icon>
                    <SemiSelect />
                  </el-icon></i>已停用</span>
              <span class="num">{{ staffData.discontinued }}</span>
            </li>
            <li class="add">
              <router-link to="/staff/add">
                <i></i>
                <p>新增医护</p>
              </router-link>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>

</template>

<script setup>
import { ref, onMounted } from 'vue'
import { businessData, orderOverView, departmentOverView, staffOverView } from '@/api/workspace';


// 获取今天的日期并格式化
const getFormattedDate = () => {
  const date = new Date();
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的，需要加1
  const day = String(date.getDate()).padStart(2, '0');

  return `${year}-${month}-${day}`;
};
// 创建一个ref来存储格式化后的日期 -- 要在getFormattedDate()之后调用
const todayDate = ref(getFormattedDate());

// 获取今日数据
const todayData = ref({})
const obtainTodayData = async () => {
  await businessData().then(res => {
    // console.log(res);
    todayData.value = res.data;
  }).catch(err => {
    // console.log(err);
    // ElMessage.error('获取今日数据失败，请稍后再试');
  });
};

// 获取订单管理数据
const ordersData = ref({})
const obtainOrderData = async () => {
  await orderOverView().then(res => {
    console.log(res);
    ordersData.value = res.data;
  }).catch(err => {
    // console.log(err);
    // ElMessage.error('获取订单数据失败，请稍后再试');
  });
};

// 获取部门总览数据
const departmentData = ref({})
const obtainDepartmentData = async () => {
  await departmentOverView().then(res => {
    console.log(res);
    departmentData.value = res.data;
  }).catch(err => {
    // console.log(err);
    // ElMessage.error('获取部门数据失败，请稍后再试');
  });
};

// 获取医护总览数据
const staffData = ref({})
const obtainStaffData = async () => {
  await staffOverView().then(res => {
    console.log(res);
    staffData.value = res.data;
  }).catch(err => {
    // console.log(err);
    // ElMessage.error('获取用户数据失败，请稍后再试');
  });
};

onMounted(() => {
   obtainTodayData()
   obtainOrderData()
   obtainDepartmentData()
   obtainStaffData()
});

</script>

<style lang="scss" scoped>
div,
h1,
h2,
h3,
h4,
h5,
h6,
li,
p,
ul {
  padding: 0;
  margin: 0;
}

li {
  list-style: none;
}

.title-index {
  .month {
    box-sizing: border-box;
    display: inline-block;
    background-color: #fff;

    ul {
      width: 400px;
      height: 40px;
      line-height: 38px;
      border: 1px solid #e5e4e4;
      border-radius: 4px;
      padding: 0;
      display: flex;
      text-align: center;
      margin: 0;

      li {
        position: relative;
        font-size: 16px;
        cursor: pointer;
        display: inline-block;
        text-align: center;
        border-right: 1px solid #e5e4e4;
        color: #333333;
        letter-spacing: -0.2px;
        flex: 1;
      }

      li:last-child {
        border: 0 none;
      }

      .active {
        height: 38px;
        background: #ffc200;
      }
    }
  }

  .get-time {
    display: inline-block;
    padding-left: 15px;
    font-size: 14px;
    color: #666;
  }
}

.sub-menu {
  position: absolute;
  width: 100%;
  left: 45%;
  bottom: 100px;
  font-size: 14px;
  color: #fff;
  font-size: 14px;
  align-items: center;
  transform: translate(-50%, -50%);
  border-top: 1px solid #4a4a4a;
  padding-top: 20px;
  padding: 20px 20px 0 50px;
  text-align: center;
  display: flex;

  .img {
    flex: 1;

    img {
      width: 20px;
      height: 15px;
    }
  }

  .avatarName {
    border-right: 1px solid #4a4a4a;
    flex: 1;
    padding: 0 15px 0 0;
  }

  .outLogin {
    cursor: pointer;
  }

  &:focus {
    outline: none;
  }

  .right-menu-item {
    display: inline-block;
    padding: 0 8px;
    height: 100%;
    font-size: 18px;
    color: #5a5e66;
    vertical-align: text-bottom;

    &.hover-effect {
      cursor: pointer;
      transition: background 0.3s;

      &:hover {
        background: rgba(0, 0, 0, 0.025);
      }
    }
  }

  .avatar-container {
    margin-right: 30px;

    .avatar-wrapper {
      margin-top: 5px;
      position: relative;

      .user-avatar {
        cursor: pointer;
        width: 40px;
        height: 40px;
        border-radius: 10px;
      }

      .el-icon-caret-bottom {
        cursor: pointer;
        position: absolute;
        right: -20px;
        top: 25px;
        font-size: 12px;
      }
    }
  }
}

// 数据概览
.overviewBox {
  ul {
    display: flex;
    text-align: left;
    margin-left: -20px;

    li {
      flex: 1;
      background: #fffbf0;
      border-radius: 4px;
      margin-left: 20px;
      padding: 20px;
    }

    .tit {
      font-size: 14px;

    }

    .num {
      font-weight: Bold;
      font-size: 28px;
      line-height: 34px;
      padding: 12px 0 10px;
      color: #333;
    }

    .tip {
      color: #666;

      &>span {
        padding-left: 5px;

        span {
          font-weight: 600;
        }
      }

      .red {
        color: #fd3333;
      }

      .green {
        color: #00B36A;
      }
    }

    i {
      color: #fd3333;
      padding-left: 8px;
      background-size: contain;
      display: inline-block;
      width: 20px;
      height: 20px;
      vertical-align: middle;
    }
  }
}

.homeTitle {
  font-weight: 600;
  font-size: 16px;
  color: #333333;
  letter-spacing: -0.2px;
  padding: 5px 0 0;
  margin-bottom: 18px;

  i {
    font-size: 14px;
    color: #666666;
    padding-left: 10px;
    font-style: normal;
    font-weight: normal
  }

  span {
    float: right;
    color: #666;
    font-size: 14px;
    font-weight: normal;
    background: url(../../assets/icons/icon_more@2x.png) no-repeat 100% 50%;
    background-size: contain;
    padding-right: 20px;
    line-height: 16px;
  }
}

.home {
  .container {
    padding: 20px;

    .homeTitleBtn {
      line-height: 36px;
    }
  }
}

.homeMain {
  display: flex;

  .container {
    flex: 1;

    &:last-child {
      margin-left: 20px;
    }
  }
}

.orderProportion {
  display: flex;
  padding-bottom: 10px;
  color: #666;
  vertical-align: middle;
  font-size: 12px;

  &>div {
    display: inline-block;

    p {
      &:last-child {
        font-size: 16px;
        font-weight: 700;
        color: #000;
        padding-top: 5px;
      }
    }
  }

  .symbol {
    padding: 10px 20px 0;
    font-size: 16px;
  }
}

.dashboard {
  &-container {

    .container {
      position: relative;
      z-index: 1;
      background: #fff;
      padding: 30px;
      border-radius: 4px;
    }

    &.home {
      .container {
        padding: 20px;
        margin-bottom: 20px;
      }

      .top10 {
        padding-bottom: 0;
      }
    }
  }
}

.orderviewBox {
  ul {
    display: flex;
    text-align: left;
    margin-left: -20px;

    li {
      flex: 1;
      background: #fffbf0;
      border-radius: 4px;
      margin-left: 20px;
      padding: 20px;
      font-size: 14px;
      line-height: 36px;
      color: #333;

      &.add {
        width: 100px;
        flex: none;
        background: #FFC100;
        border-radius: 4px;
        text-align: center;
        color: #333;
        padding: 18px 20px 10px;

        p {
          line-height: 20px;
          color: #333;
        }

        i {
          display: block;
          width: 22px;
          height: 22px;
          margin: 0 auto;
          background: url(../../assets/icons/btn_add@2x.png) no-repeat;
          background-size: contain;
          margin-bottom: 5px;
        }
      }
    }

    .status {

      vertical-align: middle;
      line-height: 36px;

      i {
        display: inline-block;
        vertical-align: middle;
        margin-right: 4px;
      }
    }

    .num {
      font-weight: Bold;
      font-size: 20px;
      float: right;
      color: #333;
      margin-top: 5px;
    }

    .tip {
      color: #fd3333;
    }
  }

  .iconfont {
    margin-bottom: 10px;
    font-size: 28px;
  }
}
</style>